home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / programming / c / make-3.75 / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-07  |  54.4 KB  |  2,099 lines

  1. /* Reading and parsing of makefiles for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24.  
  25. /* This is POSIX.2, but most systems using -DPOSIX probably don't have it.  */
  26. #ifdef    HAVE_GLOB_H
  27. #include <glob.h>
  28. #else
  29. #include "glob/glob.h"
  30. #endif
  31.  
  32. #ifndef _AMIGA
  33. #include <pwd.h>
  34. struct passwd *getpwnam ();
  35. #endif
  36.  
  37. static int read_makefile ();
  38. static unsigned int readline (), do_define ();
  39. static int conditional_line ();
  40. static void record_files ();
  41. static char *find_semicolon ();
  42.  
  43.  
  44. /* A `struct linebuffer' is a structure which holds a line of text.
  45.    `readline' reads a line from a stream into a linebuffer
  46.    and works regardless of the length of the line.  */
  47.  
  48. struct linebuffer
  49.   {
  50.     /* Note:  This is the number of bytes malloc'ed for `buffer'
  51.        It does not indicate `buffer's real length.
  52.        Instead, a null char indicates end-of-string.  */
  53.     unsigned int size;
  54.     char *buffer;
  55.   };
  56.  
  57. #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
  58. #define freebuffer(lb) free ((lb)->buffer)
  59.  
  60.  
  61. /* A `struct conditionals' contains the information describing
  62.    all the active conditionals in a makefile.
  63.  
  64.    The global variable `conditionals' contains the conditionals
  65.    information for the current makefile.  It is initialized from
  66.    the static structure `toplevel_conditionals' and is later changed
  67.    to new structures for included makefiles.  */
  68.  
  69. struct conditionals
  70.   {
  71.     unsigned int if_cmds;    /* Depth of conditional nesting.  */
  72.     unsigned int allocated;    /* Elts allocated in following arrays.    */
  73.     char *ignoring;        /* Are we ignoring or interepreting?  */
  74.     char *seen_else;        /* Have we already seen an `else'?  */
  75.   };
  76.  
  77. static struct conditionals toplevel_conditionals;
  78. static struct conditionals *conditionals = &toplevel_conditionals;
  79.  
  80.  
  81. /* Default directories to search for include files in  */
  82.  
  83. static char *default_include_directories[] =
  84.   {
  85.     INCLUDEDIR,
  86. #ifndef _AMIGA
  87.     "/usr/gnu/include",
  88.     "/usr/local/include",
  89.     "/usr/include",
  90. #endif
  91.     0
  92.   };
  93.  
  94. /* List of directories to search for include files in  */
  95.  
  96. static char **include_directories;
  97.  
  98. /* Maximum length of an element of the above.  */
  99.  
  100. static unsigned int max_incl_len;
  101.  
  102. /* The filename and pointer to line number of the
  103.    makefile currently being read in.  */
  104.  
  105. char *reading_filename;
  106. unsigned int *reading_lineno_ptr;
  107.  
  108. /* The chain of makefiles read by read_makefile.  */
  109.  
  110. static struct dep *read_makefiles = 0;
  111.  
  112. /* Read in all the makefiles and return the chain of their names.  */
  113.  
  114. struct dep *
  115. read_all_makefiles (makefiles)
  116.      char **makefiles;
  117. {
  118.   unsigned int num_makefiles = 0;
  119.  
  120.   if (debug_flag)
  121.     puts ("Reading makefiles...");
  122.  
  123.   /* If there's a non-null variable MAKEFILES, its value is a list of
  124.      files to read first thing.  But don't let it prevent reading the
  125.      default makefiles and don't let the default goal come from there.  */
  126.  
  127.   {
  128.     char *value;
  129.     char *name, *p;
  130.     unsigned int length;
  131.  
  132.     {
  133.       /* Turn off --warn-undefined-variables while we expand MAKEFILES.  */
  134.       int save = warn_undefined_variables_flag;
  135.       warn_undefined_variables_flag = 0;
  136.  
  137.       value = allocated_variable_expand ("$(MAKEFILES)");
  138.  
  139.       warn_undefined_variables_flag = save;
  140.     }
  141.  
  142.     /* Set NAME to the start of next token and LENGTH to its length.
  143.        MAKEFILES is updated for finding remaining tokens.  */
  144.     p = value;
  145.     while ((name = find_next_token (&p, &length)) != 0)
  146.       {
  147.     if (*p != '\0')
  148.       *p++ = '\0';
  149.     (void) read_makefile (name,
  150.                   RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
  151.       }
  152.  
  153.     free (value);
  154.   }
  155.  
  156.   /* Read makefiles specified with -f switches.  */
  157.  
  158.   if (makefiles != 0)
  159.     while (*makefiles != 0)
  160.       {
  161.     struct dep *tail = read_makefiles;
  162.     register struct dep *d;
  163.  
  164.     if (! read_makefile (*makefiles, 0))
  165.       perror_with_name ("", *makefiles);
  166.  
  167.     /* Find the right element of read_makefiles.  */
  168.     d = read_makefiles;
  169.     while (d->next != tail)
  170.       d = d->next;
  171.  
  172.     /* Use the storage read_makefile allocates.  */
  173.     *makefiles = dep_name (d);
  174.     ++num_makefiles;
  175.     ++makefiles;
  176.       }
  177.  
  178.   /* If there were no -f switches, try the default names.  */
  179.  
  180.   if (num_makefiles == 0)
  181.     {
  182.       static char *default_makefiles[] =
  183. #ifndef _AMIGA
  184.     { "GNUmakefile", "Makefile", "makefile", 0 };
  185. #else
  186.     { "Makefile", "SMakefile", "GNUmakefile", 0 };
  187. #endif
  188.       register char **p = default_makefiles;
  189.       while (*p != 0 && !file_exists_p (*p))
  190.     ++p;
  191.  
  192.       if (*p != 0)
  193.     {
  194.       if (! read_makefile (*p, 0))
  195.         perror_with_name ("", *p);
  196.     }
  197.       else
  198.     {
  199.       /* No default makefile was found.  Add the default makefiles to the
  200.          `read_makefiles' chain so they will be updated if possible.  */
  201.       struct dep *tail = read_makefiles;
  202.       for (p = default_makefiles; *p != 0; ++p)
  203.         {
  204.           struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
  205.           d->name = 0;
  206.           d->file = enter_file (*p);
  207.           d->file->dontcare = 1;
  208.           /* Tell update_goal_chain to bail out as soon as this file is
  209.          made, and main not to die if we can't make this file.  */
  210.           d->changed = RM_DONTCARE;
  211.           if (tail == 0)
  212.         read_makefiles = d;
  213.           else
  214.         tail->next = d;
  215.           tail = d;
  216.         }
  217.       if (tail != 0)
  218.         tail->next = 0;
  219.     }
  220.     }
  221.  
  222.   return read_makefiles;
  223. }
  224.  
  225. /* Read file FILENAME as a makefile and add its contents to the data base.
  226.  
  227.    FLAGS contains bits as above.
  228.  
  229.    FILENAME is added to the `read_makefiles' chain.
  230.  
  231.    Returns 1 if a file was found and read, 0 if not.  */
  232.  
  233. static int
  234. read_makefile (filename, flags)
  235.      char *filename;
  236.      int flags;
  237. {
  238.   static char *collapsed = 0;
  239.   static unsigned int collapsed_length = 0;
  240.   register FILE *infile;
  241.   struct linebuffer lb;
  242.   unsigned int commands_len = 200;
  243.   char *commands = (char *) xmalloc (200);
  244.   unsigned int commands_idx = 0;
  245.   unsigned int commands_started;
  246.   register char *p;
  247.   char *p2;
  248.   int ignoring = 0, in_ignored_define = 0;
  249.   int no_targets = 0;        /* Set when reading a rule without targets.  */
  250.   char *passed_filename = filename;
  251.  
  252.   struct nameseq *filenames = 0;
  253.   struct dep *deps;
  254.   unsigned int lineno = 1;
  255.   unsigned int nlines = 0;
  256.   int two_colon = 0;
  257.   char *pattern = 0, *pattern_percent = 0;
  258.  
  259.   int makefile_errno;
  260.  
  261. #define record_waiting_files()                                                \
  262.   do                                          \
  263.     {                                          \
  264.       if (filenames != 0)                                                     \
  265.     record_files (filenames, pattern, pattern_percent, deps,              \
  266.               commands_started, commands, commands_idx,           \
  267.               two_colon, filename, lineno,                  \
  268.               !(flags & RM_NO_DEFAULT_GOAL));                         \
  269.       filenames = 0;                                  \
  270.       commands_idx = 0;                               \
  271.       pattern = 0;                                  \
  272.     } while (0)
  273.  
  274. #ifdef    lint    /* Suppress `used before set' messages.  */
  275.   two_colon = 0;
  276. #endif
  277.  
  278.   if (debug_flag)
  279.     {
  280.       printf ("Reading makefile `%s'", filename);
  281.       if (flags & RM_NO_DEFAULT_GOAL)
  282.     printf (" (no default goal)");
  283.       if (flags & RM_INCLUDED)
  284.     printf (" (search path)");
  285.       if (flags & RM_DONTCARE)
  286.     printf (" (don't care)");
  287.       if (flags & RM_NO_TILDE)
  288.     printf (" (no ~ expansion)");
  289.       puts ("...");
  290.     }
  291.  
  292.   /* First, get a stream to read.  */
  293.  
  294.   /* Expand ~ in FILENAME unless it came from `include',
  295.      in which case it was already done.  */
  296.   if (!(flags & RM_NO_TILDE) && filename[0] == '~')
  297.     {
  298.       char *expanded = tilde_expand (filename);
  299.       if (expanded != 0)
  300.     filename = expanded;
  301.     }
  302.  
  303.   infile = fopen (filename, "r");
  304.   /* Save the error code so we print the right message later.  */
  305.   makefile_errno = errno;
  306.  
  307.   /* If the makefile wasn't found and it's either a makefile from
  308.      the `MAKEFILES' variable or an included makefile,
  309.      search the included makefile search path for this makefile.  */
  310.  
  311.   if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
  312.     {
  313.       register unsigned int i;
  314.       for (i = 0; include_directories[i] != 0; ++i)
  315.     {
  316.       char *name = concat (include_directories[i], "/", filename);
  317.       infile = fopen (name, "r");
  318.       if (infile == 0)
  319.         free (name);
  320.       else
  321.         {
  322.           filename = name;
  323.           break;
  324.         }
  325.     }
  326.     }
  327.  
  328.   /* Add FILENAME to the chain of read makefiles.  */
  329.   deps = (struct dep *) xmalloc (sizeof (struct dep));
  330.   deps->next = read_makefiles;
  331.   read_makefiles = deps;
  332.   deps->name = 0;
  333.   deps->file = lookup_file (filename);
  334.   if (deps->file == 0)
  335.     {
  336.       deps->file = enter_file (savestring (filename, strlen (filename)));
  337.       if (flags & RM_DONTCARE)
  338.     deps->file->dontcare = 1;
  339.     }
  340.   if (filename != passed_filename)
  341.     free (filename);
  342.   filename = deps->file->name;
  343.   deps->changed = flags;
  344.   deps = 0;
  345.  
  346.   /* If the makefile can't be found at all, give up entirely.  */
  347.  
  348.   if (infile == 0)
  349.     {
  350.       /* If we did some searching, errno has the error from the last
  351.      attempt, rather from FILENAME itself.    Restore it in case the
  352.      caller wants to use it in a message.  */
  353.       errno = makefile_errno;
  354.       return 0;
  355.     }
  356.  
  357.   reading_filename = filename;
  358.   reading_lineno_ptr = &lineno;
  359.  
  360.   /* Loop over lines in the file.
  361.      The strategy is to accumulate target names in FILENAMES, dependencies
  362.      in DEPS and commands in COMMANDS.    These are used to define a rule
  363.      when the start of the next rule (or eof) is encountered.  */
  364.  
  365.   initbuffer (&lb);
  366.  
  367.   while (!feof (infile))
  368.     {
  369.       lineno += nlines;
  370.       nlines = readline (&lb, infile, filename, lineno);
  371.  
  372.       /* Check for a shell command line first.
  373.      If it is not one, we can stop treating tab specially.    */
  374.       if (lb.buffer[0] == '\t')
  375.     {
  376.       /* This line is a probably shell command.  */
  377.       unsigned int len;
  378.  
  379.       if (no_targets)
  380.         /* Ignore the commands in a rule with no targets.  */
  381.         continue;
  382.  
  383.       /* If there is no preceding rule line, don't treat this line
  384.          as a command, even though it begins with a tab character.
  385.          SunOS 4 make appears to behave this way.  */
  386.  
  387.       if (filenames != 0)
  388.         {
  389.           if (ignoring)
  390.         /* Yep, this is a shell command, and we don't care.  */
  391.         continue;
  392.  
  393.           /* Append this command line to the line being accumulated.  */
  394.           p = lb.buffer;
  395.           if (commands_idx == 0)
  396.         commands_started = lineno;
  397.           len = strlen (p);
  398.           if (len + 1 + commands_idx > commands_len)
  399.         {
  400.           commands_len = (len + 1 + commands_idx) * 2;
  401.           commands = (char *) xrealloc (commands, commands_len);
  402.         }
  403.           bcopy (p, &commands[commands_idx], len);
  404.           commands_idx += len;
  405.           commands[commands_idx++] = '\n';
  406.  
  407.           continue;
  408.         }
  409.     }
  410.  
  411.       /* This line is not a shell command line.  Don't worry about tabs.  */
  412.  
  413.       if (collapsed_length < lb.size)
  414.     {
  415.       collapsed_length = lb.size;
  416.       if (collapsed != 0)
  417.         free (collapsed);
  418.       collapsed = (char *) xmalloc (collapsed_length);
  419.     }
  420.       strcpy (collapsed, lb.buffer);
  421.       /* Collapse continuation lines.  */
  422.       collapse_continuations (collapsed);
  423.       remove_comments (collapsed);
  424.  
  425.       /* strncmp is first to avoid dereferencing out into space.  */
  426. #define word1eq(s, l)   (!strncmp (s, p, l) \
  427.              && (p[l] == '\0' || isblank (p[l])))
  428.       p = collapsed;
  429.       while (isspace (*p))
  430.     ++p;
  431.       if (*p == '\0')
  432.     /* This line is completely empty.  */
  433.     continue;
  434.  
  435.       /* We must first check for conditional and `define' directives before
  436.      ignoring anything, since they control what we will do with
  437.      following lines.  */
  438.  
  439.       if (!in_ignored_define
  440.       && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
  441.           || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
  442.           || word1eq ("else", 4) || word1eq ("endif", 5)))
  443.     {
  444.       int i = conditional_line (p, filename, lineno);
  445.       if (i >= 0)
  446.         ignoring = i;
  447.       else
  448.         makefile_fatal (filename, lineno,
  449.                 "invalid syntax in conditional");
  450.       continue;
  451.     }
  452.       else if (word1eq ("endef", 5))
  453.     {
  454.       if (in_ignored_define)
  455.         in_ignored_define = 0;
  456.       else
  457.         makefile_fatal (filename, lineno, "extraneous `endef'");
  458.       continue;
  459.     }
  460.       else if (word1eq ("define", 6))
  461.     {
  462.       if (ignoring)
  463.         in_ignored_define = 1;
  464.       else
  465.         {
  466.           p2 = next_token (p + 6);
  467.           /* Let the variable name be the whole rest of the line,
  468.          with trailing blanks stripped (comments have already been
  469.          removed), so it could be a complex variable/function
  470.          reference that might contain blanks.  */
  471.           p = index (p2, '\0');
  472.           while (isblank (p[-1]))
  473.         --p;
  474.           lineno = do_define (p2, p - p2, o_file,
  475.                   lineno, infile, filename);
  476.         }
  477.       continue;
  478.     }
  479.       else if (word1eq ("override", 8))
  480.     {
  481.       p2 = next_token (p + 8);
  482.       if (p2 == 0)
  483.         makefile_error (filename, lineno, "empty `override' directive");
  484.       if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
  485.         {
  486.           if (ignoring)
  487.         in_ignored_define = 1;
  488.           else
  489.         {
  490.           p2 = next_token (p2 + 6);
  491.           /* Let the variable name be the whole rest of the line,
  492.              with trailing blanks stripped (comments have already been
  493.              removed), so it could be a complex variable/function
  494.              reference that might contain blanks.  */
  495.           p = index (p2, '\0');
  496.           while (isblank (p[-1]))
  497.             --p;
  498.           lineno = do_define (p2, p - p2, o_override,
  499.                       lineno, infile, filename);
  500.         }
  501.         }
  502.       else if (!ignoring
  503.            && !try_variable_definition (filename, lineno,
  504.                         p2, o_override))
  505.         makefile_error (filename, lineno, "empty `override' directive");
  506.  
  507.       continue;
  508.     }
  509.  
  510.       if (ignoring)
  511.     /* Ignore the line.  We continue here so conditionals
  512.        can appear in the middle of a rule.    */
  513.     continue;
  514.       else if (word1eq ("export", 6))
  515.     {
  516.       struct variable *v;
  517.       p2 = next_token (p + 6);
  518.       if (*p2 == '\0')
  519.         export_all_variables = 1;
  520.       v = try_variable_definition (filename, lineno, p2, o_file);
  521.       if (v != 0)
  522.         v->export = v_export;
  523.       else
  524.         {
  525.           unsigned int len;
  526.           for (p = find_next_token (&p2, &len); p != 0;
  527.            p = find_next_token (&p2, &len))
  528.         {
  529.           v = lookup_variable (p, len);
  530.           if (v == 0)
  531.             v = define_variable (p, len, "", o_file, 0);
  532.           v->export = v_export;
  533.         }
  534.         }
  535.     }
  536.       else if (word1eq ("unexport", 8))
  537.     {
  538.       unsigned int len;
  539.       struct variable *v;
  540.       p2 = next_token (p + 8);
  541.       if (*p2 == '\0')
  542.         export_all_variables = 0;
  543.       for (p = find_next_token (&p2, &len); p != 0;
  544.            p = find_next_token (&p2, &len))
  545.         {
  546.           v = lookup_variable (p, len);
  547.           if (v == 0)
  548.         v = define_variable (p, len, "", o_file, 0);
  549.           v->export = v_noexport;
  550.         }
  551.     }
  552.       else if (word1eq ("include", 7) || word1eq ("-include", 8))
  553.     {
  554.       /* We have found an `include' line specifying a nested
  555.          makefile to be read at this point.  */
  556.       struct conditionals *save, new_conditionals;
  557.       struct nameseq *files;
  558.       /* "-include" (vs "include") says no
  559.          error if the file does not exist.    */
  560.       int noerror = p[0] == '-';
  561.  
  562.       p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
  563.       if (*p == '\0')
  564.         {
  565.           makefile_error (filename, lineno,
  566.                   "no file name for `%sinclude'",
  567.                   noerror ? "-" : "");
  568.           continue;
  569.         }
  570.  
  571.       /* Parse the list of file names.  */
  572.       p2 = p;
  573.       files = multi_glob (parse_file_seq (&p2, '\0',
  574.                           sizeof (struct nameseq),
  575.                           1),
  576.                   sizeof (struct nameseq));
  577.       free (p);
  578.  
  579.       /* Save the state of conditionals and start
  580.          the included makefile with a clean slate.    */
  581.       save = conditionals;
  582.       bzero ((char *) &new_conditionals, sizeof new_conditionals);
  583.       conditionals = &new_conditionals;
  584.  
  585.       /* Record the rules that are waiting so they will determine
  586.          the default goal before those in the included makefile.  */
  587.       record_waiting_files ();
  588.  
  589.       /* Read each included makefile.  */
  590.       while (files != 0)
  591.         {
  592.           struct nameseq *next = files->next;
  593.           char *name = files->name;
  594.           free (files);
  595.           files = next;
  596.  
  597.           if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
  598.                       | (noerror ? RM_DONTCARE : 0)))
  599.           && ! noerror)
  600.         makefile_error (filename, lineno,
  601.                 "%s: %s", name, strerror (errno));
  602.         }
  603.  
  604.       /* Free any space allocated by conditional_line.  */
  605.       if (conditionals->ignoring)
  606.         free (conditionals->ignoring);
  607.       if (conditionals->seen_else)
  608.         free (conditionals->seen_else);
  609.  
  610.       /* Restore state.  */
  611.       conditionals = save;
  612.       reading_filename = filename;
  613.       reading_lineno_ptr = &lineno;
  614.     }
  615.       else if (word1eq ("vpath", 5))
  616.     {
  617.       char *pattern;
  618.       unsigned int len;
  619.       p2 = variable_expand (p + 5);
  620.       p = find_next_token (&p2, &len);
  621.       if (p != 0)
  622.         {
  623.           pattern = savestring (p, len);
  624.           p = find_next_token (&p2, &len);
  625.           /* No searchpath means remove all previous
  626.          selective VPATH's with the same pattern.  */
  627.         }
  628.       else
  629.         /* No pattern means remove all previous selective VPATH's.  */
  630.         pattern = 0;
  631.       construct_vpath_list (pattern, p);
  632.       if (pattern != 0)
  633.         free (pattern);
  634.     }
  635. #undef    word1eq
  636.       else if (try_variable_definition (filename, lineno, p, o_file))
  637.     /* This line has been dealt with.  */
  638.     ;
  639.       else if (lb.buffer[0] == '\t')
  640.     {
  641.       p = collapsed;    /* Ignore comments.  */
  642.       while (isblank (*p))
  643.         ++p;
  644.       if (*p == '\0')
  645.         /* The line is completely blank; that is harmless.    */
  646.         continue;
  647.       /* This line starts with a tab but was not caught above
  648.          because there was no preceding target, and the line
  649.          might have been usable as a variable definition.
  650.          But now it is definitely lossage.    */
  651.       makefile_fatal (filename, lineno,
  652.               "commands commence before first target");
  653.     }
  654.       else
  655.     {
  656.       /* This line describes some target files.  */
  657.  
  658.       char *cmdleft;
  659.  
  660.       /* Record the previous rule.    */
  661.  
  662.       record_waiting_files ();
  663.  
  664.       /* Look for a semicolon in the unexpanded line.  */
  665.       cmdleft = find_semicolon (lb.buffer);
  666.       if (cmdleft != 0)
  667.         /* Found one.  Cut the line short there before expanding it.  */
  668.         *cmdleft = '\0';
  669.  
  670.       collapse_continuations (lb.buffer);
  671.  
  672.       /* Expand variable and function references before doing anything
  673.          else so that special characters can be inside variables.  */
  674.       p = variable_expand (lb.buffer);
  675.  
  676.       if (cmdleft == 0)
  677.         /* Look for a semicolon in the expanded line.  */
  678.         cmdleft = find_semicolon (p);
  679.  
  680.       if (cmdleft != 0)
  681.         /* Cut the line short at the semicolon.  */
  682.         *cmdleft = '\0';
  683.  
  684.       /* Remove comments from the line.  */
  685.       remove_comments (p);
  686.  
  687.       p2 = next_token (p);
  688.       if (*p2 == '\0')
  689.         {
  690.           if (cmdleft != 0)
  691.         makefile_fatal (filename, lineno,
  692.                 "missing rule before commands");
  693.           else
  694.         /* This line contained a variable reference that
  695.            expanded to nothing but whitespace.    */
  696.         continue;
  697.         }
  698.       else if (*p2 == ':')
  699.         {
  700.           /* We accept and ignore rules without targets for
  701.          compatibility with SunOS 4 make.  */
  702.           no_targets = 1;
  703.           continue;
  704.         }
  705.  
  706.       filenames = multi_glob (parse_file_seq (&p2, ':',
  707.                           sizeof (struct nameseq),
  708.                           1),
  709.                   sizeof (struct nameseq));
  710.       if (*p2++ == '\0')
  711.         makefile_fatal (filename, lineno, "missing separator");
  712.       /* Is this a one-colon or two-colon entry?  */
  713.       two_colon = *p2 == ':';
  714.       if (two_colon)
  715.         p2++;
  716.  
  717.       /* We have some targets, so don't ignore the following commands.  */
  718.       no_targets = 0;
  719.  
  720.       /* Is this a static pattern rule: `target: %targ: %dep; ...'?  */
  721.       p = index (p2, ':');
  722.       while (p != 0 && p[-1] == '\\')
  723.         {
  724.           register char *q = &p[-1];
  725.           register int backslash = 0;
  726.           while (*q-- == '\\')
  727.         backslash = !backslash;
  728.           if (backslash)
  729.         p = index (p + 1, ':');
  730.           else
  731.         break;
  732.         }
  733. #ifdef __MSDOS__
  734.       /* For MS-DOS, skip a "C:\...".  */
  735.       if (p != 0 && p[1] == '\\' && isalpha (p[-1]))
  736.         p = 0;
  737. #endif
  738. #ifdef _AMIGA
  739.       /* Here, the situation is quite complicated. Let's have a look
  740.         at a couple of targets:
  741.  
  742.         install: dev:make
  743.  
  744.         dev:make: make
  745.  
  746.         dev:make:: xyz
  747.  
  748.         The rule is that it's only a target, if there are TWO :'s
  749.         OR a space around the :.
  750.       */
  751.       if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
  752.         p = 0;
  753. #endif
  754.       if (p != 0)
  755.         {
  756.           struct nameseq *target;
  757.           target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
  758.           ++p2;
  759.           if (target == 0)
  760.         makefile_fatal (filename, lineno, "missing target pattern");
  761.           else if (target->next != 0)
  762.         makefile_fatal (filename, lineno, "multiple target patterns");
  763.           pattern = target->name;
  764.           pattern_percent = find_percent (pattern);
  765.           if (pattern_percent == 0)
  766.         makefile_fatal (filename, lineno,
  767.                 "target pattern contains no `%%'");
  768.         }
  769.       else
  770.         pattern = 0;
  771.  
  772.       /* Parse the dependencies.  */
  773.       deps = (struct dep *)
  774.         multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
  775.             sizeof (struct dep));
  776.  
  777.       commands_idx = 0;
  778.       if (cmdleft != 0)
  779.         {
  780.           /* Semicolon means rest of line is a command.  */
  781.           unsigned int len = strlen (cmdleft + 1);
  782.  
  783.           commands_started = lineno;
  784.  
  785.           /* Add this command line to the buffer.  */
  786.           if (len + 2 > commands_len)
  787.         {
  788.           commands_len = (len + 2) * 2;
  789.           commands = (char *) xrealloc (commands, commands_len);
  790.         }
  791.           bcopy (cmdleft + 1, commands, len);
  792.           commands_idx += len;
  793.           commands[commands_idx++] = '\n';
  794.         }
  795.  
  796.       continue;
  797.     }
  798.  
  799.       /* We get here except in the case that we just read a rule line.
  800.      Record now the last rule we read, so following spurious
  801.      commands are properly diagnosed.  */
  802.       record_waiting_files ();
  803.       no_targets = 0;
  804.     }
  805.  
  806.   if (conditionals->if_cmds)
  807.     makefile_fatal (filename, lineno, "missing `endif'");
  808.  
  809.   /* At eof, record the last rule.  */
  810.   record_waiting_files ();
  811.  
  812.   freebuffer (&lb);
  813.   free ((char *) commands);
  814.   fclose (infile);
  815.  
  816.   reading_filename = 0;
  817.   reading_lineno_ptr = 0;
  818.  
  819.   return 1;
  820. }
  821.  
  822. /* Execute a `define' directive.
  823.    The first line has already been read, and NAME is the name of
  824.    the variable to be defined.    The following lines remain to be read.
  825.    LINENO, INFILE and FILENAME refer to the makefile being read.
  826.    The value returned is LINENO, updated for lines read here.  */
  827.  
  828. static unsigned int
  829. do_define (name, namelen, origin, lineno, infile, filename)
  830.      char *name;
  831.      unsigned int namelen;
  832.      enum variable_origin origin;
  833.      unsigned int lineno;
  834.      FILE *infile;
  835.      char *filename;
  836. {
  837.   struct linebuffer lb;
  838.   unsigned int nlines = 0;
  839.   unsigned int length = 100;
  840.   char *definition = (char *) xmalloc (100);
  841.   register unsigned int idx = 0;
  842.   register char *p;
  843.  
  844.   /* Expand the variable name.    */
  845.   char *var = (char *) alloca (namelen + 1);
  846.   bcopy (name, var, namelen);
  847.   var[namelen] = '\0';
  848.   var = variable_expand (var);
  849.  
  850.   initbuffer (&lb);
  851.   while (!feof (infile))
  852.     {
  853.       lineno += nlines;
  854.       nlines = readline (&lb, infile, filename, lineno);
  855.  
  856.       collapse_continuations (lb.buffer);
  857.  
  858.       p = next_token (lb.buffer);
  859.       if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
  860.     {
  861.       p += 5;
  862.       remove_comments (p);
  863.       if (*next_token (p) != '\0')
  864.         makefile_error (filename, lineno,
  865.                 "Extraneous text after `endef' directive");
  866.       /* Define the variable.  */
  867.       if (idx == 0)
  868.         definition[0] = '\0';
  869.       else
  870.         definition[idx - 1] = '\0';
  871.       (void) define_variable (var, strlen (var), definition, origin, 1);
  872.       free (definition);
  873.       freebuffer (&lb);
  874.       return lineno;
  875.     }
  876.       else
  877.     {
  878.       unsigned int len = strlen (lb.buffer);
  879.  
  880.       /* Increase the buffer size if necessary.  */
  881.       if (idx + len + 1 > length)
  882.         {
  883.           length = (idx + len) * 2;
  884.           definition = (char *) xrealloc (definition, length + 1);
  885.         }
  886.  
  887.       bcopy (lb.buffer, &definition[idx], len);
  888.       idx += len;
  889.       /* Separate lines with a newline.  */
  890.       definition[idx++] = '\n';
  891.     }
  892.     }
  893.  
  894.   /* No `endef'!!  */
  895.   makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
  896.  
  897.   /* NOTREACHED */
  898.   return 0;
  899. }
  900.  
  901. /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
  902.    "ifneq", "else" and "endif".
  903.    LINE is the input line, with the command as its first word.
  904.  
  905.    FILENAME and LINENO are the filename and line number in the
  906.    current makefile.  They are used for error messages.
  907.  
  908.    Value is -1 if the line is invalid,
  909.    0 if following text should be interpreted,
  910.    1 if following text should be ignored.  */
  911.  
  912. static int
  913. conditional_line (line, filename, lineno)
  914.      char *line;
  915.      char *filename;
  916.      unsigned int lineno;
  917. {
  918.   int notdef;
  919.   char *cmdname;
  920.   register unsigned int i;
  921.  
  922.   if (*line == 'i')
  923.     {
  924.       /* It's an "if..." command.  */
  925.       notdef = line[2] == 'n';
  926.       if (notdef)
  927.     {
  928.       cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
  929.       line += cmdname[3] == 'd' ? 7 : 6;
  930.     }
  931.       else
  932.     {
  933.       cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
  934.       line += cmdname[2] == 'd' ? 6 : 5;
  935.     }
  936.     }
  937.   else
  938.     {
  939.       /* It's an "else" or "endif" command.  */
  940.       notdef = line[1] == 'n';
  941.       cmdname = notdef ? "endif" : "else";
  942.       line += notdef ? 5 : 4;
  943.     }
  944.  
  945.   line = next_token (line);
  946.  
  947.   if (*cmdname == 'e')
  948.     {
  949.       if (*line != '\0')
  950.     makefile_error (filename, lineno,
  951.             "Extraneous text after `%s' directive",
  952.             cmdname);
  953.       /* "Else" or "endif".  */
  954.       if (conditionals->if_cmds == 0)
  955.     makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
  956.       /* NOTDEF indicates an `endif' command.  */
  957.       if (notdef)
  958.     --conditionals->if_cmds;
  959.       else if (conditionals->seen_else[conditionals->if_cmds - 1])
  960.     makefile_fatal (filename, lineno, "only one `else' per conditional");
  961.       else
  962.     {
  963.       /* Toggle the state of ignorance.  */
  964.       conditionals->ignoring[conditionals->if_cmds - 1]
  965.         = !conditionals->ignoring[conditionals->if_cmds - 1];
  966.       /* Record that we have seen an `else' in this conditional.
  967.          A second `else' will be erroneous.  */
  968.       conditionals->seen_else[conditionals->if_cmds - 1] = 1;
  969.     }
  970.       for (i = 0; i < conditionals->if_cmds; ++i)
  971.     if (conditionals->ignoring[i])
  972.       return 1;
  973.       return 0;
  974.     }
  975.  
  976.   if (conditionals->allocated == 0)
  977.     {
  978.       conditionals->allocated = 5;
  979.       conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
  980.       conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
  981.     }
  982.  
  983.   ++conditionals->if_cmds;
  984.   if (conditionals->if_cmds > conditionals->allocated)
  985.     {
  986.       conditionals->allocated += 5;
  987.       conditionals->ignoring = (char *)
  988.     xrealloc (conditionals->ignoring, conditionals->allocated);
  989.       conditionals->seen_else = (char *)
  990.     xrealloc (conditionals->seen_else, conditionals->allocated);
  991.     }
  992.  
  993.   /* Record that we have seen an `if...' but no `else' so far.  */
  994.   conditionals->seen_else[conditionals->if_cmds - 1] = 0;
  995.  
  996.   /* Search through the stack to see if we're already ignoring.  */
  997.   for (i = 0; i < conditionals->if_cmds - 1; ++i)
  998.     if (conditionals->ignoring[i])
  999.       {
  1000.     /* We are already ignoring, so just push a level
  1001.        to match the next "else" or "endif", and keep ignoring.
  1002.        We don't want to expand variables in the condition.  */
  1003.     conditionals->ignoring[conditionals->if_cmds - 1] = 1;
  1004.     return 1;
  1005.       }
  1006.  
  1007.   if (cmdname[notdef ? 3 : 2] == 'd')
  1008.     {
  1009.       /* "Ifdef" or "ifndef".  */
  1010.       struct variable *v;
  1011.       register char *p = end_of_token (line);
  1012.       i = p - line;
  1013.       p = next_token (p);
  1014.       if (*p != '\0')
  1015.     return -1;
  1016.       v = lookup_variable (line, i);
  1017.       conditionals->ignoring[conditionals->if_cmds - 1]
  1018.     = (v != 0 && *v->value != '\0') == notdef;
  1019.     }
  1020.   else
  1021.     {
  1022.       /* "Ifeq" or "ifneq".  */
  1023.       char *s1, *s2;
  1024.       unsigned int len;
  1025.       char termin = *line == '(' ? ',' : *line;
  1026.  
  1027.       if (termin != ',' && termin != '"' && termin != '\'')
  1028.     return -1;
  1029.  
  1030.       s1 = ++line;
  1031.       /* Find the end of the first string.  */
  1032.       if (termin == ',')
  1033.     {
  1034.       register int count = 0;
  1035.       for (; *line != '\0'; ++line)
  1036.         if (*line == '(')
  1037.           ++count;
  1038.         else if (*line == ')')
  1039.           --count;
  1040.         else if (*line == ',' && count <= 0)
  1041.           break;
  1042.     }
  1043.       else
  1044.     while (*line != '\0' && *line != termin)
  1045.       ++line;
  1046.  
  1047.       if (*line == '\0')
  1048.     return -1;
  1049.  
  1050.       *line++ = '\0';
  1051.  
  1052.       s2 = variable_expand (s1);
  1053.       /* We must allocate a new copy of the expanded string because
  1054.      variable_expand re-uses the same buffer.  */
  1055.       len = strlen (s2);
  1056.       s1 = (char *) alloca (len + 1);
  1057.       bcopy (s2, s1, len + 1);
  1058.  
  1059.       if (termin != ',')
  1060.     /* Find the start of the second string.  */
  1061.     line = next_token (line);
  1062.  
  1063.       termin = termin == ',' ? ')' : *line;
  1064.       if (termin != ')' && termin != '"' && termin != '\'')
  1065.     return -1;
  1066.  
  1067.       /* Find the end of the second string.  */
  1068.       if (termin == ')')
  1069.     {
  1070.       register int count = 0;
  1071.       s2 = next_token (line);
  1072.       for (line = s2; *line != '\0'; ++line)
  1073.         {
  1074.           if (*line == '(')
  1075.         ++count;
  1076.           else if (*line == ')')
  1077.         if (count <= 0)
  1078.           break;
  1079.         else
  1080.           --count;
  1081.         }
  1082.     }
  1083.       else
  1084.     {
  1085.       ++line;
  1086.       s2 = line;
  1087.       while (*line != '\0' && *line != termin)
  1088.         ++line;
  1089.     }
  1090.  
  1091.       if (*line == '\0')
  1092.     return -1;
  1093.  
  1094.       *line = '\0';
  1095.       line = next_token (++line);
  1096.       if (*line != '\0')
  1097.     makefile_error (filename, lineno,
  1098.             "Extraneous text after `%s' directive",
  1099.             cmdname);
  1100.  
  1101.       s2 = variable_expand (s2);
  1102.       conditionals->ignoring[conditionals->if_cmds - 1]
  1103.     = streq (s1, s2) == notdef;
  1104.     }
  1105.  
  1106.   /* Search through the stack to see if we're ignoring.  */
  1107.   for (i = 0; i < conditionals->if_cmds; ++i)
  1108.     if (conditionals->ignoring[i])
  1109.       return 1;
  1110.   return 0;
  1111. }
  1112.  
  1113. /* Remove duplicate dependencies in CHAIN.  */
  1114.  
  1115. void
  1116. uniquize_deps (chain)
  1117.      struct dep *chain;
  1118. {
  1119.   register struct dep *d;
  1120.  
  1121.   /* Make sure that no dependencies are repeated.  This does not
  1122.      really matter for the purpose of updating targets, but it
  1123.      might make some names be listed twice for $^ and $?.  */
  1124.  
  1125.   for (d = chain; d != 0; d = d->next)
  1126.     {
  1127.       struct dep *last, *next;
  1128.  
  1129.       last = d;
  1130.       next = d->next;
  1131.       while (next != 0)
  1132.     if (streq (dep_name (d), dep_name (next)))
  1133.       {
  1134.         struct dep *n = next->next;
  1135.         last->next = n;
  1136.         if (next->name != 0 && next->name != d->name)
  1137.           free (next->name);
  1138.         if (next != d)
  1139.           free ((char *) next);
  1140.         next = n;
  1141.       }
  1142.     else
  1143.       {
  1144.         last = next;
  1145.         next = next->next;
  1146.       }
  1147.     }
  1148. }
  1149.  
  1150. /* Record a description line for files FILENAMES,
  1151.    with dependencies DEPS, commands to execute described
  1152.    by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
  1153.    TWO_COLON is nonzero if a double colon was used.
  1154.    If not nil, PATTERN is the `%' pattern to make this
  1155.    a static pattern rule, and PATTERN_PERCENT is a pointer
  1156.    to the `%' within it.
  1157.  
  1158.    The links of FILENAMES are freed, and so are any names in it
  1159.    that are not incorporated into other data structures.  */
  1160.  
  1161. static void
  1162. record_files (filenames, pattern, pattern_percent, deps, commands_started,
  1163.           commands, commands_idx, two_colon, filename, lineno, set_default)
  1164.      struct nameseq *filenames;
  1165.      char *pattern, *pattern_percent;
  1166.      struct dep *deps;
  1167.      unsigned int commands_started;
  1168.      char *commands;
  1169.      unsigned int commands_idx;
  1170.      int two_colon;
  1171.      char *filename;
  1172.      unsigned int lineno;
  1173.      int set_default;
  1174. {
  1175.   struct nameseq *nextf;
  1176.   int implicit = 0;
  1177.   unsigned int max_targets, target_idx;
  1178.   char **targets = 0, **target_percents = 0;
  1179.   struct commands *cmds;
  1180.  
  1181.   if (commands_idx > 0)
  1182.     {
  1183.       cmds = (struct commands *) xmalloc (sizeof (struct commands));
  1184.       cmds->filename = filename;
  1185.       cmds->lineno = commands_started;
  1186.       cmds->commands = savestring (commands, commands_idx);
  1187.       cmds->command_lines = 0;
  1188.     }
  1189.   else
  1190.     cmds = 0;
  1191.  
  1192.   for (; filenames != 0; filenames = nextf)
  1193.     {
  1194.       register char *name = filenames->name;
  1195.       register struct file *f;
  1196.       register struct dep *d;
  1197.       struct dep *this;
  1198.       char *implicit_percent;
  1199.  
  1200.       nextf = filenames->next;
  1201.       free ((char *) filenames);
  1202.  
  1203.       implicit_percent = find_percent (name);
  1204.       implicit |= implicit_percent != 0;
  1205.  
  1206.       if (implicit && pattern != 0)
  1207.     makefile_fatal (filename, lineno,
  1208.             "mixed implicit and static pattern rules");
  1209.  
  1210.       if (implicit && implicit_percent == 0)
  1211.     makefile_fatal (filename, lineno, "mixed implicit and normal rules");
  1212.  
  1213.       if (implicit)
  1214.     {
  1215.       if (targets == 0)
  1216.         {
  1217.           max_targets = 5;
  1218.           targets = (char **) xmalloc (5 * sizeof (char *));
  1219.           target_percents = (char **) xmalloc (5 * sizeof (char *));
  1220.           target_idx = 0;
  1221.         }
  1222.       else if (target_idx == max_targets - 1)
  1223.         {
  1224.           max_targets += 5;
  1225.           targets = (char **) xrealloc ((char *) targets,
  1226.                         max_targets * sizeof (char *));
  1227.           target_percents
  1228.         = (char **) xrealloc ((char *) target_percents,
  1229.                       max_targets * sizeof (char *));
  1230.         }
  1231.       targets[target_idx] = name;
  1232.       target_percents[target_idx] = implicit_percent;
  1233.       ++target_idx;
  1234.       continue;
  1235.     }
  1236.  
  1237.       /* If there are multiple filenames, copy the chain DEPS
  1238.      for all but the last one.  It is not safe for the same deps
  1239.      to go in more than one place in the data base.  */
  1240.       this = nextf != 0 ? copy_dep_chain (deps) : deps;
  1241.  
  1242.       if (pattern != 0)
  1243.     /* If this is an extended static rule:
  1244.        `targets: target%pattern: dep%pattern; cmds',
  1245.        translate each dependency pattern into a plain filename
  1246.        using the target pattern and this target's name.  */
  1247.     if (!pattern_matches (pattern, pattern_percent, name))
  1248.       {
  1249.         /* Give a warning if the rule is meaningless.  */
  1250.         makefile_error (filename, lineno,
  1251.                 "target `%s' doesn't match the target pattern",
  1252.                 name);
  1253.         this = 0;
  1254.       }
  1255.     else
  1256.       {
  1257.         /* We use patsubst_expand to do the work of translating
  1258.            the target pattern, the target's name and the dependencies'
  1259.            patterns into plain dependency names.  */
  1260.         char *buffer = variable_expand ("");
  1261.  
  1262.         for (d = this; d != 0; d = d->next)
  1263.           {
  1264.         char *o;
  1265.         char *percent = find_percent (d->name);
  1266.         if (percent == 0)
  1267.           continue;
  1268.         o = patsubst_expand (buffer, name, pattern, d->name,
  1269.                      pattern_percent, percent);
  1270.         free (d->name);
  1271.         d->name = savestring (buffer, o - buffer);
  1272.           }
  1273.       }
  1274.  
  1275.       if (!two_colon)
  1276.     {
  1277.       /* Single-colon.  Combine these dependencies
  1278.          with others in file's existing record, if any.  */
  1279.       f = enter_file (name);
  1280.  
  1281.       if (f->double_colon)
  1282.         makefile_fatal (filename, lineno,
  1283.                 "target file `%s' has both : and :: entries",
  1284.                 f->name);
  1285.  
  1286.       /* If CMDS == F->CMDS, this target was listed in this rule
  1287.          more than once.  Just give a warning since this is harmless.  */
  1288.       if (cmds != 0 && cmds == f->cmds)
  1289.         makefile_error
  1290.           (filename, lineno,
  1291.            "target `%s' given more than once in the same rule.",
  1292.            f->name);
  1293.  
  1294.       /* Check for two single-colon entries both with commands.
  1295.          Check is_target so that we don't lose on files such as .c.o
  1296.          whose commands were preinitialized.  */
  1297.       else if (cmds != 0 && f->cmds != 0 && f->is_target)
  1298.         {
  1299.           makefile_error (cmds->filename, cmds->lineno,
  1300.                   "warning: overriding commands for target `%s'",
  1301.                   f->name);
  1302.           makefile_error (f->cmds->filename, f->cmds->lineno,
  1303.                   "warning: ignoring old commands for target `%s'",
  1304.                   f->name);
  1305.         }
  1306.  
  1307.       f->is_target = 1;
  1308.  
  1309.       /* Defining .DEFAULT with no deps or cmds clears it.    */
  1310.       if (f == default_file && this == 0 && cmds == 0)
  1311.         f->cmds = 0;
  1312.       if (cmds != 0)
  1313.         f->cmds = cmds;
  1314.       /* Defining .SUFFIXES with no dependencies
  1315.          clears out the list of suffixes.  */
  1316.       if (f == suffix_file && this == 0)
  1317.         {
  1318.           d = f->deps;
  1319.           while (d != 0)
  1320.         {
  1321.           struct dep *nextd = d->next;
  1322.           free (d->name);
  1323.           free (d);
  1324.           d = nextd;
  1325.         }
  1326.           f->deps = 0;
  1327.         }
  1328.       else if (f->deps != 0)
  1329.         {
  1330.           /* Add the file's old deps and the new ones in THIS together.  */
  1331.  
  1332.           struct dep *firstdeps, *moredeps;
  1333.           if (cmds != 0)
  1334.         {
  1335.           /* This is the rule with commands, so put its deps first.
  1336.              The rationale behind this is that $< expands to the
  1337.              first dep in the chain, and commands use $< expecting
  1338.              to get the dep that rule specifies.  */
  1339.           firstdeps = this;
  1340.           moredeps = f->deps;
  1341.         }
  1342.           else
  1343.         {
  1344.           /* Append the new deps to the old ones.  */
  1345.           firstdeps = f->deps;
  1346.           moredeps = this;
  1347.         }
  1348.  
  1349.           if (firstdeps == 0)
  1350.         firstdeps = moredeps;
  1351.           else
  1352.         {
  1353.           d = firstdeps;
  1354.           while (d->next != 0)
  1355.             d = d->next;
  1356.           d->next = moredeps;
  1357.         }
  1358.  
  1359.           f->deps = firstdeps;
  1360.         }
  1361.       else
  1362.         f->deps = this;
  1363.  
  1364.       /* If this is a static pattern rule, set the file's stem to
  1365.          the part of its name that matched the `%' in the pattern,
  1366.          so you can use $* in the commands.  */
  1367.       if (pattern != 0)
  1368.         {
  1369.           static char *percent = "%";
  1370.           char *buffer = variable_expand ("");
  1371.           char *o = patsubst_expand (buffer, name, pattern, percent,
  1372.                      pattern_percent, percent);
  1373.           f->stem = savestring (buffer, o - buffer);
  1374.         }
  1375.     }
  1376.       else
  1377.     {
  1378.       /* Double-colon.  Make a new record
  1379.          even if the file already has one.    */
  1380.       f = lookup_file (name);
  1381.       /* Check for both : and :: rules.  Check is_target so
  1382.          we don't lose on default suffix rules or makefiles.  */
  1383.       if (f != 0 && f->is_target && !f->double_colon)
  1384.         makefile_fatal (filename, lineno,
  1385.                 "target file `%s' has both : and :: entries",
  1386.                 f->name);
  1387.       f = enter_file (name);
  1388.       /* If there was an existing entry and it was a double-colon
  1389.          entry, enter_file will have returned a new one, making it the
  1390.          prev pointer of the old one, and setting its double_colon
  1391.          pointer to the first one.    */
  1392.       if (f->double_colon == 0)
  1393.         /* This is the first entry for this name, so we must
  1394.            set its double_colon pointer to itself.    */
  1395.         f->double_colon = f;
  1396.       f->is_target = 1;
  1397.       f->deps = this;
  1398.       f->cmds = cmds;
  1399.     }
  1400.  
  1401.       /* Free name if not needed further.  */
  1402.       if (f != 0 && name != f->name
  1403.       && (name < f->name || name > f->name + strlen (f->name)))
  1404.     {
  1405.       free (name);
  1406.       name = f->name;
  1407.     }
  1408.  
  1409.       /* See if this is first target seen whose name does
  1410.      not start with a `.', unless it contains a slash.  */
  1411.       if (default_goal_file == 0 && set_default
  1412.       && (*name != '.' || index (name, '/') != 0))
  1413.     {
  1414.       int reject = 0;
  1415.  
  1416.       /* If this file is a suffix, don't
  1417.          let it be the default goal file.  */
  1418.  
  1419.       for (d = suffix_file->deps; d != 0; d = d->next)
  1420.         {
  1421.           register struct dep *d2;
  1422.           if (*dep_name (d) != '.' && streq (name, dep_name (d)))
  1423.         {
  1424.           reject = 1;
  1425.           break;
  1426.         }
  1427.           for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
  1428.         {
  1429.           register unsigned int len = strlen (dep_name (d2));
  1430.           if (strncmp (name, dep_name (d2), len))
  1431.             continue;
  1432.           if (streq (name + len, dep_name (d)))
  1433.             {
  1434.               reject = 1;
  1435.               break;
  1436.             }
  1437.         }
  1438.           if (reject)
  1439.         break;
  1440.         }
  1441.  
  1442.       if (!reject)
  1443.         default_goal_file = f;
  1444.     }
  1445.     }
  1446.  
  1447.   if (implicit)
  1448.     {
  1449.       targets[target_idx] = 0;
  1450.       target_percents[target_idx] = 0;
  1451.       create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
  1452.       free ((char *) target_percents);
  1453.     }
  1454. }
  1455.  
  1456. /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
  1457.    Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
  1458.    Quoting backslashes are removed from STRING by compacting it into
  1459.    itself.  Returns a pointer to the first unquoted STOPCHAR if there is
  1460.    one, or nil if there are none.  */
  1461.  
  1462. char *
  1463. find_char_unquote (string, stopchars, blank)
  1464.      char *string;
  1465.      char *stopchars;
  1466.      int blank;
  1467. {
  1468.   unsigned int string_len = strlen (string);
  1469.   register char *p = string;
  1470.  
  1471.   while (1)
  1472.     {
  1473.       while (*p != '\0' && index (stopchars, *p) == 0
  1474.          && (!blank || !isblank (*p)))
  1475.     ++p;
  1476.       if (*p == '\0')
  1477.     break;
  1478.  
  1479.       if (p > string && p[-1] == '\\')
  1480.     {
  1481.       /* Search for more backslashes.  */
  1482.       register int i = -2;
  1483.       while (&p[i] >= string && p[i] == '\\')
  1484.         --i;
  1485.       ++i;
  1486.       /* The number of backslashes is now -I.
  1487.          Copy P over itself to swallow half of them.  */
  1488.       bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
  1489.       p += i / 2;
  1490.       if (i % 2 == 0)
  1491.         /* All the backslashes quoted each other; the STOPCHAR was
  1492.            unquoted.  */
  1493.         return p;
  1494.  
  1495.       /* The STOPCHAR was quoted by a backslash.  Look for another.  */
  1496.     }
  1497.       else
  1498.     /* No backslash in sight.  */
  1499.     return p;
  1500.     }
  1501.  
  1502.   /* Never hit a STOPCHAR or blank (with BLANK nonzero).  */
  1503.   return 0;
  1504. }
  1505.  
  1506. /* Search PATTERN for an unquoted %.  */
  1507.  
  1508. char *
  1509. find_percent (pattern)
  1510.      char *pattern;
  1511. {
  1512.   return find_char_unquote (pattern, "%", 0);
  1513. }
  1514.  
  1515. /* Search STRING for an unquoted ; that is not after an unquoted #.  */
  1516.  
  1517. static char *
  1518. find_semicolon (string)
  1519.      char *string;
  1520. {
  1521.   char *match = find_char_unquote (string, ";#", 0);
  1522.   if (match != 0 && *match == '#')
  1523.     /* We found a comment before a semicolon.  No match.  */
  1524.     match = 0;
  1525.   return match;
  1526. }
  1527.  
  1528. /* Parse a string into a sequence of filenames represented as a
  1529.    chain of struct nameseq's in reverse order and return that chain.
  1530.  
  1531.    The string is passed as STRINGP, the address of a string pointer.
  1532.    The string pointer is updated to point at the first character
  1533.    not parsed, which either is a null char or equals STOPCHAR.
  1534.  
  1535.    SIZE is how big to construct chain elements.
  1536.    This is useful if we want them actually to be other structures
  1537.    that have room for additional info.
  1538.  
  1539.    If STRIP is nonzero, strip `./'s off the beginning.  */
  1540.  
  1541. struct nameseq *
  1542. parse_file_seq (stringp, stopchar, size, strip)
  1543.      char **stringp;
  1544.      char stopchar;
  1545.      unsigned int size;
  1546.      int strip;
  1547. {
  1548.   register struct nameseq *new = 0;
  1549.   register struct nameseq *new1, *lastnew1;
  1550.   register char *p = *stringp;
  1551.   char *q;
  1552.   char *name;
  1553.   char stopchars[2];
  1554.   stopchars[0] = stopchar;
  1555.   stopchars[1] = '\0';
  1556.  
  1557.   while (1)
  1558.     {
  1559.       /* Skip whitespace; see if any more names are left.  */
  1560.       p = next_token (p);
  1561.       if (*p == '\0')
  1562.     break;
  1563.       if (*p == stopchar)
  1564.     break;
  1565.       /* Yes, find end of next name.  */
  1566.       q = p;
  1567.       p = find_char_unquote (q, stopchars, 1);
  1568. #ifdef __MSDOS__
  1569.       /* For MS-DOS, skip a "C:\...".  */
  1570.       if (stopchar == ':' && p != 0 && p[1] == '\\' && isalpha (p[-1]))
  1571.     p = 0;
  1572. #endif
  1573. #ifdef _AMIGA
  1574.       if (stopchar == ':' && p && *p == ':' &&
  1575.     !(isspace(p[1]) || !p[1] || isspace(p[-1])))
  1576.       {
  1577.     p = find_char_unquote (p+1, stopchars, 1);
  1578.       }
  1579. #endif
  1580.       if (p == 0)
  1581.     p = q + strlen (q);
  1582.  
  1583.       if (strip)
  1584.     /* Skip leading `./'s.  */
  1585.     while (p - q > 2 && q[0] == '.' && q[1] == '/')
  1586.       {
  1587.         q += 2;        /* Skip "./".  */
  1588.         while (q < p && *q == '/')
  1589.           /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  1590.           ++q;
  1591.       }
  1592.  
  1593.       /* Extract the filename just found, and skip it.    */
  1594.  
  1595.       if (q == p)
  1596.     /* ".///" was stripped to "".  */
  1597. #ifdef _AMIGA
  1598.     name = savestring ("", 0);
  1599. #else
  1600.     name = savestring ("./", 2);
  1601. #endif
  1602.       else
  1603.     name = savestring (q, p - q);
  1604.  
  1605.       /* Add it to the front of the chain.  */
  1606.       new1 = (struct nameseq *) xmalloc (size);
  1607.       new1->name = name;
  1608.       new1->next = new;
  1609.       new = new1;
  1610.     }
  1611.  
  1612. #ifndef NO_ARCHIVES
  1613.  
  1614.   /* Look for multi-word archive references.
  1615.      They are indicated by a elt ending with an unmatched `)' and
  1616.      an elt further down the chain (i.e., previous in the file list)
  1617.      with an unmatched `(' (e.g., "lib(mem").  */
  1618.  
  1619.   new1 = new;
  1620.   lastnew1 = 0;
  1621.   while (new1 != 0)
  1622.     if (new1->name[0] != '('    /* Don't catch "(%)" and suchlike.  */
  1623.     && new1->name[strlen (new1->name) - 1] == ')'
  1624.     && index (new1->name, '(') == 0)
  1625.       {
  1626.     /* NEW1 ends with a `)' but does not contain a `('.
  1627.        Look back for an elt with an opening `(' but no closing `)'.  */
  1628.  
  1629.     struct nameseq *n = new1->next, *lastn = new1;
  1630.     char *paren;
  1631.     while (n != 0 && (paren = index (n->name, '(')) == 0)
  1632.       {
  1633.         lastn = n;
  1634.         n = n->next;
  1635.       }
  1636.     if (n != 0
  1637.         /* Ignore something starting with `(', as that cannot actually
  1638.            be an archive-member reference (and treating it as such
  1639.            results in an empty file name, which causes much lossage).  */
  1640.         && n->name[0] != '(')
  1641.       {
  1642.         /* N is the first element in the archive group.
  1643.            Its name looks like "lib(mem" (with no closing `)').  */
  1644.  
  1645.         char *libname;
  1646.  
  1647.         /* Copy "lib(" into LIBNAME.  */
  1648.         ++paren;
  1649.         libname = (char *) alloca (paren - n->name + 1);
  1650.         bcopy (n->name, libname, paren - n->name);
  1651.         libname[paren - n->name] = '\0';
  1652.  
  1653.         if (*paren == '\0')
  1654.           {
  1655.         /* N was just "lib(", part of something like "lib( a b)".
  1656.            Edit it out of the chain and free its storage.  */
  1657.         lastn->next = n->next;
  1658.         free (n->name);
  1659.         free ((char *) n);
  1660.         /* LASTN->next is the new stopping elt for the loop below.  */
  1661.         n = lastn->next;
  1662.           }
  1663.         else
  1664.           {
  1665.         /* Replace N's name with the full archive reference.  */
  1666.         name = concat (libname, paren, ")");
  1667.         free (n->name);
  1668.         n->name = name;
  1669.           }
  1670.  
  1671.         if (new1->name[1] == '\0')
  1672.           {
  1673.         /* NEW1 is just ")", part of something like "lib(a b )".
  1674.            Omit it from the chain and free its storage.  */
  1675.         if (lastnew1 == 0)
  1676.           new = new1->next;
  1677.         else
  1678.           lastnew1->next = new1->next;
  1679.         lastn = new1;
  1680.         new1 = new1->next;
  1681.         free (lastn->name);
  1682.         free ((char *) lastn);
  1683.           }
  1684.         else
  1685.           {
  1686.         /* Replace also NEW1->name, which already has closing `)'.  */
  1687.         name = concat (libname, new1->name, "");
  1688.         free (new1->name);
  1689.         new1->name = name;
  1690.         new1 = new1->next;
  1691.           }
  1692.  
  1693.         /* Trace back from NEW1 (the end of the list) until N
  1694.            (the beginning of the list), rewriting each name
  1695.            with the full archive reference.  */
  1696.  
  1697.         while (new1 != n)
  1698.           {
  1699.         name = concat (libname, new1->name, ")");
  1700.         free (new1->name);
  1701.         new1->name = name;
  1702.         lastnew1 = new1;
  1703.         new1 = new1->next;
  1704.           }
  1705.       }
  1706.     else
  1707.       {
  1708.         /* No frobnication happening.  Just step down the list.  */
  1709.         lastnew1 = new1;
  1710.         new1 = new1->next;
  1711.       }
  1712.       }
  1713.     else
  1714.       {
  1715.     lastnew1 = new1;
  1716.     new1 = new1->next;
  1717.       }
  1718.  
  1719. #endif
  1720.  
  1721.   *stringp = p;
  1722.   return new;
  1723. }
  1724.  
  1725. /* Read a line of text from STREAM into LINEBUFFER.
  1726.    Combine continuation lines into one line.
  1727.    Return the number of actual lines read (> 1 if hacked continuation lines).
  1728.  */
  1729.  
  1730. static unsigned int
  1731. readline (linebuffer, stream, filename, lineno)
  1732.      struct linebuffer *linebuffer;
  1733.      FILE *stream;
  1734.      char *filename;
  1735.      unsigned int lineno;
  1736. {
  1737.   char *buffer = linebuffer->buffer;
  1738.   register char *p = linebuffer->buffer;
  1739.   register char *end = p + linebuffer->size;
  1740.   register int len, lastlen = 0;
  1741.   register char *p2;
  1742.   register unsigned int nlines = 0;
  1743.   register int backslash;
  1744.  
  1745.   *p = '\0';
  1746.  
  1747.   while (fgets (p, end - p, stream) != 0)
  1748.     {
  1749.       len = strlen (p);
  1750.       if (len == 0)
  1751.     {
  1752.       /* This only happens when the first thing on the line is a '\0'.
  1753.          It is a pretty hopeless case, but (wonder of wonders) Athena
  1754.          lossage strikes again!  (xmkmf puts NULs in its makefiles.)
  1755.          There is nothing really to be done; we synthesize a newline so
  1756.          the following line doesn't appear to be part of this line.  */
  1757.       makefile_error (filename, lineno,
  1758.               "warning: NUL character seen; rest of line ignored");
  1759.       p[0] = '\n';
  1760.       len = 1;
  1761.     }
  1762.  
  1763.       p += len;
  1764.       if (p[-1] != '\n')
  1765.     {
  1766.       /* Probably ran out of buffer space.    */
  1767.       register unsigned int p_off = p - buffer;
  1768.       linebuffer->size *= 2;
  1769.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1770.       p = buffer + p_off;
  1771.       end = buffer + linebuffer->size;
  1772.       linebuffer->buffer = buffer;
  1773.       *p = '\0';
  1774.       lastlen = len;
  1775.       continue;
  1776.     }
  1777.  
  1778.       ++nlines;
  1779.  
  1780.       if (len == 1 && p > buffer)
  1781.     /* P is pointing at a newline and it's the beginning of
  1782.        the buffer returned by the last fgets call.    However,
  1783.        it is not necessarily the beginning of a line if P is
  1784.        pointing past the beginning of the holding buffer.
  1785.        If the buffer was just enlarged (right before the newline),
  1786.        we must account for that, so we pretend that the two lines
  1787.        were one line.  */
  1788.     len += lastlen;
  1789.       lastlen = len;
  1790.       backslash = 0;
  1791.       for (p2 = p - 2; --len > 0; --p2)
  1792.     {
  1793.       if (*p2 == '\\')
  1794.         backslash = !backslash;
  1795.       else
  1796.         break;
  1797.     }
  1798.  
  1799.       if (!backslash)
  1800.     {
  1801.       p[-1] = '\0';
  1802.       break;
  1803.     }
  1804.  
  1805.       if (end - p <= 1)
  1806.     {
  1807.       /* Enlarge the buffer.  */
  1808.       register unsigned int p_off = p - buffer;
  1809.       linebuffer->size *= 2;
  1810.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1811.       p = buffer + p_off;
  1812.       end = buffer + linebuffer->size;
  1813.       linebuffer->buffer = buffer;
  1814.     }
  1815.     }
  1816.  
  1817.   if (ferror (stream))
  1818.     pfatal_with_name (filename);
  1819.  
  1820.   return nlines;
  1821. }
  1822.  
  1823. /* Construct the list of include directories
  1824.    from the arguments and the default list.  */
  1825.  
  1826. void
  1827. construct_include_path (arg_dirs)
  1828.      char **arg_dirs;
  1829. {
  1830.   register unsigned int i;
  1831.   struct stat stbuf;
  1832.  
  1833.   /* Table to hold the dirs.  */
  1834.  
  1835.   register unsigned int defsize = (sizeof (default_include_directories)
  1836.                    / sizeof (default_include_directories[0]));
  1837.   register unsigned int max = 5;
  1838.   register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
  1839.   register unsigned int idx = 0;
  1840.  
  1841.   /* First consider any dirs specified with -I switches.
  1842.      Ignore dirs that don't exist.  */
  1843.  
  1844.   if (arg_dirs != 0)
  1845.     while (*arg_dirs != 0)
  1846.       {
  1847.     char *dir = *arg_dirs++;
  1848.  
  1849.     if (dir[0] == '~')
  1850.       {
  1851.         char *expanded = tilde_expand (dir);
  1852.         if (expanded != 0)
  1853.           dir = expanded;
  1854.       }
  1855.  
  1856.     if (safe_stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
  1857.       {
  1858.         if (idx == max - 1)
  1859.           {
  1860.         max += 5;
  1861.         dirs = (char **)
  1862.           xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
  1863.           }
  1864.         dirs[idx++] = dir;
  1865.       }
  1866.     else if (dir != arg_dirs[-1])
  1867.       free (dir);
  1868.       }
  1869.  
  1870.   /* Now add at the end the standard default dirs.  */
  1871.  
  1872.   for (i = 0; default_include_directories[i] != 0; ++i)
  1873.     if (safe_stat (default_include_directories[i], &stbuf) == 0
  1874.     && S_ISDIR (stbuf.st_mode))
  1875.       dirs[idx++] = default_include_directories[i];
  1876.  
  1877.   dirs[idx] = 0;
  1878.  
  1879.   /* Now compute the maximum length of any name in it.    */
  1880.  
  1881.   max_incl_len = 0;
  1882.   for (i = 0; i < idx; ++i)
  1883.     {
  1884.       unsigned int len = strlen (dirs[i]);
  1885.       /* If dir name is written with a trailing slash, discard it.  */
  1886.       if (dirs[i][len - 1] == '/')
  1887.     /* We can't just clobber a null in because it may have come from
  1888.        a literal string and literal strings may not be writable.  */
  1889.     dirs[i] = savestring (dirs[i], len - 1);
  1890.       if (len > max_incl_len)
  1891.     max_incl_len = len;
  1892.     }
  1893.  
  1894.   include_directories = dirs;
  1895. }
  1896.  
  1897. /* Expand ~ or ~USER at the beginning of NAME.
  1898.    Return a newly malloc'd string or 0.  */
  1899.  
  1900. char *
  1901. tilde_expand (name)
  1902.      char *name;
  1903. {
  1904.   if (name[1] == '/' || name[1] == '\0')
  1905.     {
  1906.       extern char *getenv ();
  1907.       char *home_dir;
  1908.       int is_variable;
  1909.  
  1910.       {
  1911.     /* Turn off --warn-undefined-variables while we expand HOME.  */
  1912.     int save = warn_undefined_variables_flag;
  1913.     warn_undefined_variables_flag = 0;
  1914.  
  1915.     home_dir = allocated_variable_expand ("$(HOME)");
  1916.  
  1917.     warn_undefined_variables_flag = save;
  1918.       }
  1919.  
  1920.       is_variable = home_dir[0] != '\0';
  1921.       if (!is_variable)
  1922.     {
  1923.       free (home_dir);
  1924.       home_dir = getenv ("HOME");
  1925.     }
  1926. #ifndef _AMIGA
  1927.       if (home_dir == 0 || home_dir[0] == '\0')
  1928.     {
  1929.       extern char *getlogin ();
  1930.       char *name = getlogin ();
  1931.       home_dir = 0;
  1932.       if (name != 0)
  1933.         {
  1934.           struct passwd *p = getpwnam (name);
  1935.           if (p != 0)
  1936.         home_dir = p->pw_dir;
  1937.         }
  1938.     }
  1939. #endif
  1940.       if (home_dir != 0)
  1941.     {
  1942.       char *new = concat (home_dir, "", name + 1);
  1943.       if (is_variable)
  1944.         free (home_dir);
  1945.       return new;
  1946.     }
  1947.     }
  1948. #ifndef _AMIGA
  1949.   else
  1950.     {
  1951.       struct passwd *pwent;
  1952.       char *userend = index (name + 1, '/');
  1953.       if (userend != 0)
  1954.     *userend = '\0';
  1955.       pwent = getpwnam (name + 1);
  1956.       if (pwent != 0)
  1957.     {
  1958.       if (userend == 0)
  1959.         return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
  1960.       else
  1961.         return concat (pwent->pw_dir, "/", userend + 1);
  1962.     }
  1963.       else if (userend != 0)
  1964.     *userend = '/';
  1965.     }
  1966. #endif
  1967.  
  1968.   return 0;
  1969. }
  1970.  
  1971. /* Given a chain of struct nameseq's describing a sequence of filenames,
  1972.    in reverse of the intended order, return a new chain describing the
  1973.    result of globbing the filenames.  The new chain is in forward order.
  1974.    The links of the old chain are freed or used in the new chain.
  1975.    Likewise for the names in the old chain.
  1976.  
  1977.    SIZE is how big to construct chain elements.
  1978.    This is useful if we want them actually to be other structures
  1979.    that have room for additional info.    */
  1980.  
  1981. struct nameseq *
  1982. multi_glob (chain, size)
  1983.      struct nameseq *chain;
  1984.      unsigned int size;
  1985. {
  1986.   register struct nameseq *new = 0;
  1987.   register struct nameseq *old;
  1988.   struct nameseq *nexto;
  1989.  
  1990.   for (old = chain; old != 0; old = nexto)
  1991.     {
  1992.       glob_t gl;
  1993. #ifndef NO_ARCHIVES
  1994.       char *memname;
  1995. #endif
  1996.  
  1997.       nexto = old->next;
  1998.  
  1999.       if (old->name[0] == '~')
  2000.     {
  2001.       char *newname = tilde_expand (old->name);
  2002.       if (newname != 0)
  2003.         {
  2004.           free (old->name);
  2005.           old->name = newname;
  2006.         }
  2007.     }
  2008.  
  2009. #ifndef NO_ARCHIVES
  2010.       if (ar_name (old->name))
  2011.     {
  2012.       /* OLD->name is an archive member reference.
  2013.          Replace it with the archive file name,
  2014.          and save the member name in MEMNAME.
  2015.          We will glob on the archive name and then
  2016.          reattach MEMNAME later.  */
  2017.       char *arname;
  2018.       ar_parse_name (old->name, &arname, &memname);
  2019.       free (old->name);
  2020.       old->name = arname;
  2021.     }
  2022.       else
  2023.     memname = 0;
  2024. #endif
  2025.  
  2026.       switch (glob (old->name, GLOB_NOCHECK, NULL, &gl))
  2027.     {
  2028.     case 0:         /* Success.  */
  2029.       {
  2030.         register int i = gl.gl_pathc;
  2031.         while (i-- > 0)
  2032.           {
  2033. #ifndef NO_ARCHIVES
  2034.         if (memname != 0)
  2035.           {
  2036.             /* Try to glob on MEMNAME within the archive.  */
  2037.             struct nameseq *found
  2038.               = ar_glob (gl.gl_pathv[i], memname, size);
  2039.             if (found == 0)
  2040.               {
  2041.             /* No matches.    Use MEMNAME as-is.  */
  2042.             struct nameseq *elt
  2043.               = (struct nameseq *) xmalloc (size);
  2044.             unsigned int alen = strlen (gl.gl_pathv[i]);
  2045.             unsigned int mlen = strlen (memname);
  2046.             elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
  2047.             bcopy (gl.gl_pathv[i], elt->name, alen);
  2048.             elt->name[alen] = '(';
  2049.             bcopy (memname, &elt->name[alen + 1], mlen);
  2050.             elt->name[alen + 1 + mlen] = ')';
  2051.             elt->name[alen + 1 + mlen + 1] = '\0';
  2052.             elt->next = new;
  2053.             new = elt;
  2054.               }
  2055.             else
  2056.               {
  2057.             /* Find the end of the FOUND chain.  */
  2058.             struct nameseq *f = found;
  2059.             while (f->next != 0)
  2060.               f = f->next;
  2061.  
  2062.             /* Attach the chain being built to the end of the FOUND
  2063.                chain, and make FOUND the new NEW chain.  */
  2064.             f->next = new;
  2065.             new = found;
  2066.               }
  2067.  
  2068.             free (memname);
  2069.           }
  2070.         else
  2071. #endif
  2072.           {
  2073.             struct nameseq *elt = (struct nameseq *) xmalloc (size);
  2074.             elt->name = savestring (gl.gl_pathv[i],
  2075.                         strlen (gl.gl_pathv[i]));
  2076.             elt->next = new;
  2077.             new = elt;
  2078.           }
  2079.           }
  2080.         globfree (&gl);
  2081.         free (old->name);
  2082.         free (old);
  2083.         break;
  2084.       }
  2085.  
  2086.     case GLOB_NOSPACE:
  2087.       fatal ("virtual memory exhausted");
  2088.       break;
  2089.  
  2090.     default:
  2091.       old->next = new;
  2092.       new = old;
  2093.       break;
  2094.     }
  2095.     }
  2096.  
  2097.   return new;
  2098. }
  2099.